package com.baihui.taobao.db;

import com.baihui.taobao.util.Util;
import com.baihui.taobao.vo.BOrder;
import com.baihui.taobao.vo.BTrade;
import com.taobao.api.TaobaoObject;
import com.taobao.api.domain.Trade;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Kevin. User: HSZM1106-0312 Date: 12-1-14 Time: 下午5:41
 */
public class OrderDAO extends GeneDAO {
	@Override
	protected String getPrimaryKey(Class aClass) {
		return "oid";
	}

	@Override
	public String[] getExcludefield() {
		return new String[] { "serialVersionUID", "baihuiid" };
	}

	@Override
	/**
	 * order关联一个trade，这里返回trade的主键类型
	 */
	protected String parseRelationType(Field field) {
		if (field.getType().equals(Trade.class)) {
			return "bigint(50)";
		}
		return null;
	}

	@Override
	protected Class getDomainClass() {
		return BOrder.class;
	}

	@Override
	protected Object parseValue(Object value, Field field) {
		if (field.getType().equals(Trade.class)) {
			return ((Trade) value).getTid();
		}
		return value;
	}

	@Override
	public List<Map> findAllObject(String sql) throws Exception {
		List<Map> orders = super.findAllObject(sql);
		ItemDao itemDao = new ItemDao();
		TradeDAO tradeDAO = new TradeDAO();
		for (Map order : orders) {
			if (order.get("numIid") != null) {
				Map<String, Object> map = itemDao.findObject(
						"select * from titem where num_iid = ? ",
						new Object[] { order.get("numIid").toString() });
				if (map == null)
					map = new HashMap<String, Object>();
				order.put("item", map);
			}

			if (order.get("parent") != null) {
				Map<String, Object> trade = tradeDAO.findObject(
						"select * from tbtrade where tid = ? ",
						new Object[] { order.get("parent") });
				if (trade == null)
					trade = new HashMap<String, Object>();
				order.put("trade", trade);
			}
		}
		return orders;
	}

	@Override
	public List<Map> findAllObject(String sql, int maxValue) throws Exception {
		List<Map> orders = super.findAllObject(sql, maxValue);
		// ItemDao itemDao = new ItemDao();
		// TradeDAO tradeDAO = new TradeDAO();
		// for (Map order : orders) {
		// if (order.get("numIid") != null) {
		// Map<String, Object> map =
		// itemDao.findObject("select * from titem where num_iid = '" +
		// order.get("numIid").toString() + "'");
		// if (map == null) map = new HashMap<String, Object>();
		// order.put("item", map);
		// }
		//
		// if (order.get("parent") != null) {
		// Map<String, Object> trade =
		// tradeDAO.findObject("select * from tbtrade where tid = " +
		// order.get("parent"));
		// if (trade == null) trade = new HashMap<String, Object>();
		// order.put("trade", trade);
		// }
		// }
		return orders;
	}

	@Override
	public Map<String, Object> findObject(String sql) throws Exception {
		Map<String, Object> order = super.findObject(sql);
		if (order.get("numIid") != null) {
			ItemDao itemDao = new ItemDao();
			Map<String, Object> map = itemDao.findObject(
					"select * from titem where num_iid = ? ",
					new Object[] { order.get("numIid").toString() });
			if (map == null)
				map = new HashMap<String, Object>();
			order.put("item", map);
		}
		if (order.get("parent") != null) {
			TradeDAO tradeDAO = new TradeDAO();
			Map<String, Object> trade = tradeDAO.findObject(
					"select * from tbtrade where tid = ? ",
					new Object[] { order.get("parent") });
			if (trade == null)
				trade = new HashMap<String, Object>();
			order.put("trade", trade);
		}
		return order;
	}

	public void insertOrder(List<BOrder> infos, Connection cn) throws Exception {
		if (infos == null || infos.size() <= 0)
			return;
		StringBuffer sql = new StringBuffer();
		sql.append(" insert into tborder(parent,adjustFee,discountFee,userId,buyerNick,num,numIid,outerIid,outerSkuId,payment,price,sellerNick,skuPropertiesName,status,exported,baihuiid,oid ) ");
		sql.append(" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");

		List<Object[]> values = new ArrayList<Object[]>();
		for (BOrder order : infos) {
			List<Object> tmp = new ArrayList<Object>();
			tmp.add(order.getParent().getTid());
			tmp.add(order.getAdjustFee());
			tmp.add(order.getDiscountFee());
			tmp.add(order.getUserId());
			tmp.add(order.getBuyerNick());
			tmp.add(order.getNum());
			tmp.add(order.getNumIid());
			tmp.add(order.getOuterIid());
			tmp.add(order.getOuterSkuId());
			tmp.add(order.getPayment());
			tmp.add(order.getPrice());
			tmp.add(order.getSellerNick());
			tmp.add(order.getSkuPropertiesName());
			tmp.add(order.getStatus());
			tmp.add(order.getExported());
			tmp.add(order.getBaihuiid());
			tmp.add(order.getOid());

			values.add(tmp.toArray(new Object[] {}));
		}

		updateAndInsert(cn, sql.toString(), values);
	}

	public void updateOrder(List<BOrder> infos, Connection cn) throws Exception {
		if (infos == null || infos.size() <= 0)
			return;
		StringBuffer sql = new StringBuffer();
		sql.append(" update tborder set status = ?,num = ?,price = ?,discountFee = ?, payment = ? ");
		sql.append(" where oid = ? ");

		List<Object[]> values = new ArrayList<Object[]>();
		for (BOrder order : infos) {
			List<Object> tmp = new ArrayList<Object>();
			tmp.add(order.getStatus());
			tmp.add(order.getNum());
			tmp.add(order.getPrice());
			tmp.add(order.getDiscountFee());
			tmp.add(order.getParent());
			tmp.add(order.getOid());

			values.add(tmp.toArray(new Object[] {}));
		}

		updateAndInsert(cn, sql.toString(), values);
	}
}
